<%@codepage="65001"%>
<%session.codepage="65001"%>
<!-- #include virtual=/inc/dbConn.asp -->
<!-- #include virtual=/inc/function.asp -->
<!-- #include virtual=/inc/action.header.inc.asp -->

<%
	'## 파라미터 변수 선언부
	rcvrId	= Request("findRcvrId")
	startDt		= Request("findStartDt")
	endDt		= Request("findEndDt")
	word		= Request("findWord")
	
	setUser	= Request("setUser")	'권한 위임

	SQLTitle = "-- ■■■■ 발송문서 그리드 List up ■■■■"	
	
	userId		= Request.Cookies("S_uid")
	
	'## 검색조건에 따른 조건절 만들기 
	If startDt <> "" Then 
		addSql = " CONVERT(varchar(10), A.writeDt, 121) >= '" & startDt & "' "
	End If 
	If endDt <> "" Then 
		If addSql <> "" Then addSql = addSql & " AND "
		addSql = " CONVERT(varchar(10), A.writeDt, 121) <= '" & endDt & "' "
	End If 
	If rcvrId <> "" Then 
		If addSql <> "" Then addSql = addSql & " AND "
		addSql = " B.rcvrId = '" & rcvrId & "' "
	End If 
	If word <> "" Then 
		If addSql <> "" Then addSql = addSql & " AND "
		addSql = addSql & " (A.subjects LIKE '%" & word & "%' OR A.contents LIKE '%" & word & "%') "
	End If 
	if setUser <> "" then 
		If addSql <> "" Then addSql = addSql & " AND "
		addSql = addSql & " A.writeId='" & setUser & "' "
	else
		If addSql <> "" Then addSql = addSql & " AND "
		addSql = addSql & " A.writeId='" & userId & "' "
	end if 
	If addSql <> "" Then addSql = " AND " & addSql
	addSql = " WHERE A.flag='Y' AND  A.tmpFlag='-'  "  & addSql

	SQL = "SELECT A.workSeq, B.rcvrId, C.userNm rcvrNm, A.endDt, CASE WHEN B.openDt IS NULL THEN '1' WHEN B.knowDt IS NULL THEN '2' WHEN B.completeDt IS NULL THEN '3' ELSE '9' END status, " & _
			"					 A.subjects, CONVERT(varchar(16), A.writeDt, 121) writeDt, CONVERT(varchar(16), A.modDt, 121) modDt, T.tailCnt " & _
			"		FROM tblWorkBbs A " & _
			"				LEFT JOIN tblWorkBbsRcvr B ON A.workSeq=B.workSeq " & _
			"				LEFT JOIN tblUser C ON B.rcvrId=C.userId " & _ 
			"				LEFT JOIN (SELECT workSeq, ISNULL(count(*),'') tailCnt FROM tblWorkBbsTail WHERE flag='Y' GROUP BY workSeq) T ON A.workSeq=T.workSeq " & addSql & _
			"	ORDER BY status, A.workSeq DESC "
			
	If xMode="debug" Then 
		Call showDebug(SQLTitle,SQL) 
	End If 

	Set Rs=Server.CreateObject("ADODB.RecordSet")
	Rs.open SQL, DbCon, 1
	
	Response.write "<SendList>" & vbcrlf

	If Rs.eof Or Rs.bof Then 
	Else 
		Do Until Rs.eof Or Rs.bof
			
			Response.write "	<SendWork>" & vbcrlf
				Call mkXMLNode("workSeq", rs("workSeq"), 2)
				Call mkXMLNode("rcvrId", rs("rcvrId"), 2)
				Call mkXMLNode("rcvrNm", rs("rcvrNm"), 2)
				Call mkXMLNode("status", rs("status"), 2)
				Call mkXMLNode("subjects", rs("subjects"), 2)
				Call mkXMLNode("writeDt", right(rs("writeDt"),11), 2)
				Call mkXMLNode("endDt", rs("endDt"), 2)
				Call mkXMLNode("modDt", right(rs("modDt"),11), 2)
				Call mkXMLNode("tailCnt", rs("tailCnt"), 2)
			Response.write "	</SendWork>" & vbcrlf

		Rs.movenext
		Loop 
	End If
	Response.write "</SendList>" & vbcrlf
'	Call rsWithXML("Save OK!")
%>